/**
 * Created by lenovo on 2017/5/1.
 */
$(function () {
    // 构建统计
    var myChart = echarts.init(document.getElementsByClassName('build-day')[0]);

    // 平均构建耗时
    var myChart2 = echarts.init(document.getElementsByClassName('build-average')[0]);

    // 健康检查
    var myChart3 = echarts.init(document.getElementsByClassName('href-circle')[0]);

    // 日均代码提交量
    var myChart4 = echarts.init(document.getElementsByClassName('rjdmtjl')[0]);

    // 开发日志
    var myChart5 = echarts.init(document.getElementsByClassName('kfrz-pie')[0]);
    //myChart5.setOption(option5);

    function getDayTime() {
        var d = new Date();
        var day = d.getHours();
        var a = [];
        for(var i = 0; i<= day; i++){
            a.push(i + ':00');
        }
        return a;
    }

    var timeCount = 5 * 1000;
    // var i = Math.floor(Math.random()*3);
     var i = 2;
    cpglFn(i);
    kfrzFn(i);
    // 构建总数
    function gjzsFn() {
        $(".gjzs span").each(function (index) {
            $(this).html(gjzsData[i][index] + new Date().getHours());
        })
        setInterval(function () {
         var index = $(".gjzs .act").index();
         index = (index + 1) % 3;
         $(".gjzs span").eq(index).addClass('act').siblings().removeClass('act');
         buildDayFn($(".gjzs .act").attr('data-day'));
         }, timeCount)
    }
    gjzsFn();
    // 构建统计
    function buildDayFn(day) {
        if(day == '1'){
            option.xAxis[0].data = getDayTime();
            /*var d1 = [], d2 = [];
            buildDayData.forEach(function (n) {
               n.value1.forEach(function (nn) {
                   d1.push(Math.ceil(nn / 7));
               })
                n.value2.forEach(function (nn) {
                    d2.push(Math.ceil(nn / 7));
                })
            })
            d2 = d2.map(function (n) {
                return {value:n,textStyle:{
                    color: "#00FFFB"
                }}
            })*/
            var dHour = parseInt(new Date().getHours()) + 1;
            option.series[0].data = buildDayData[0].value1.slice(0, dHour);
            option.series[1].data = buildDayData[0].value2.slice(0, dHour);
            var sum = option.series[0].data.reduce(function(a,b){
                    return a+b
                }) +option.series[1].data.reduce(function(a,b){
                    return a+b
                })
            console.log(sum)
            $(".gjzs span").eq(0).html(sum)

        }else if (day == '7'){
           /* option.xAxis[0].data = buildDayData[i].date.slice(-6);
            var d7 = [];
            buildDayData[i].value1.slice(-6).forEach(function (n) {
                d7.push({value:n,textStyle:{
                    color: "#00FFFB"
                }})
            })
            option.series[0].data = d7;
            option.series[1].data = buildDayData[i].value2.slice(-6);*/
            option.xAxis[0].data = buildDayData[1].date;
            option.series[0].data = buildDayData[1].value1;
            option.series[1].data = buildDayData[1].value2;
        }else if (day == '30'){
            option.xAxis[0].data = buildDayData[i].date;
            var d30 = [];
            buildDayData[i].value1.forEach(function (n) {
                d30.push({value:n,textStyle:{
                    color: "#00FFFB"
                }})
            })
            option.series[0].data = d30;
            option.series[1].data = buildDayData[i].value2;
        }

        myChart.setOption(option);
    }
    buildDayFn('1');
    // 平均构建耗时
    function buildAverageFn(date, value) {
        /*option2.xAxis[0].data = buildAverageData[i].date;
        option2.series[0].data = buildAverageData[i].value;*/
        option2.xAxis[0].data = date;
        option2.series[0].data = value;
        myChart2.setOption(option2, true);
    }
    //构建历史
    function gjlsFn() {
        var _date = [],_value=[];
        if(localStorage.getItem('goujianlishi') == null){
            var history = {};
            history.time = Date.now();
            history.data = [];
            $(".gjls tr").each(function (index) {
                var tmp = {};
                $item = $(this);
                if(index != 0){
                    tmp.imageSrc = Math.random() > .3 ? 'cg' : 'jg';
                    tmp.d1 = gjlsData[2] - index + new Date().getHours()*3;
                    tmp.d2 = index * 20 + Math.floor(Math.random() * 20);
                    if(tmp.imageSrc == 'jg'){
                        tmp.d3 = {value:Math.ceil(Math.random()* 3) + 2,itemStyle:{normal:{color:'#ED6474'}}};
                    }else{
                        tmp.d3 =Math.ceil(Math.random()* 3) + 2;
                    }
                    history.data.push(tmp)
                }
            })
            localStorage.setItem('goujianlishi', JSON.stringify(history))
        }else{
            var history = JSON.parse(localStorage.getItem('goujianlishi'));
            var min = Math.floor((Date.now() - history.time) / 60000 )
            history.data.forEach(function (ii) {
                ii.d2 = ii.d2 + min;
            })
            if(min > 20){
                for(var n = 0 ; n < min/20; n++){
                    var tmp = {};
                    $item = $(".gjls tr").eq(1);
                    tmp.imageSrc = Math.random() > .3 ? 'cg' : 'jg';
                    tmp.d1 = history.data[0].d1 + 1;
                    tmp.d2 = Math.floor(history.data[0].d2 - 15 - Math.random() * 5);
                    if(tmp.imageSrc == 'jg'){
                        tmp.d3 = {value:Math.ceil(Math.random()* 3) + 2,itemStyle:{normal:{color:'#ED6474'}}};
                    }else{
                        tmp.d3 =Math.ceil(Math.random()* 3) + 2;
                    }
                    history.data.unshift(tmp);
                    history.data.pop();
                }
            }
            history.time = Date.now();
            localStorage.setItem('goujianlishi', JSON.stringify(history))
        }


        $(".gjls tr").each(function (index) {
            $item = $(this);
            if(index != 0){
                setitem($item, index - 1)
            }
        })
        var n = gjlsData[i];
        function setitem($item, index) {
            var historyData = history.data;
            $item.find('td').eq(0).find('img').attr('src', './css/icon_'+ historyData[index].imageSrc + '.png');
            $item.find('td').eq(1).html(historyData[index].d1);
            _date.unshift(historyData[index].d1);
            var val = historyData[index].d2;
            var t = '';
            if(val > 60){
                t = Math.ceil( val/60 ) + ' hours ago';
            }else{
                t = val + ' min ago';
            }
            // 构建时间
            $item.find('td').eq(2).html(t);
            _value.unshift(historyData[index].d3);
        }
        buildAverageFn(_date, _value);

        /*setInterval(function () {
            $(".gjls tr").each(function (index) {
                $item = $(this).find('td').eq(2).find('span');
                $item.html(parseInt($item.html()) + 1);
            });
            $item = $(".gjls tr").last();
            setitem($item, 0);
            $item.prependTo($(".gjls table"));
            gjlsData[i] = gjlsData[i] + 1;
            $item.find('td').eq(1).html(gjlsData[i]);
            _date.shift();
            _date.shift();
             var type = _value.shift();
             _value.shift();
            _date.push(gjlsData[i]);
            if(typeof type == 'object'){
                _value.push({value:Math.ceil(Math.random()* 3) + 2,itemStyle:{normal:{color:'#ED6474'}}});
            }else{
                _value.push(Math.ceil(Math.random()* 3) + 2);
            }
            buildAverageFn(_date, _value);
        }, timeCount)*/
    }
    gjlsFn();


    function cpglFn(i) {
        var d = cpglData[i];
        $(".xqzl em").each(function (index) {
            $(this).html(d.xqzl[index])
        })
        $(".yfryzl em").each(function (index) {
            $(this).html(d.yfryzl[index])
        })
        $(".qxzl em").each(function (index) {
            $(this).html(d.qxzl[index])
        })
    }
    // 开发日志
    function kfrzFn(i) {
        $(".kfrz p").each(function (index) {
            $(this).html(kfrzData[i][index].info + '<i>' + kfrzData[i][index].time + '</i>')
        })
        $(".kfrz img").each(function (index) {
            $(this).attr('src', './css/icon_' + kfrzData[i][index].type + '.png')
        })
    }
    setInterval(function () {
        kfrzData[i].unshift(kfrzData[i].pop());
        kfrzFn(i);
    }, timeCount)
    // 监控
    function jiankFn() {
        var buildTime = [12, 8, 20];
        var names = ['dev1', 'dev2', 'sa2', 'dsa2', 'aeb2', 'aev2', 'dev3'];
        if(localStorage.getItem('jiankong') == null){
            var historys = {};
            historys.time = Date.now();
            historys.data = [];
            $(".jiank table tr").each(function (index) {
                historys.data.push(getData(index))
            });
            localStorage.setItem('jiankong', JSON.stringify(historys))
        }else{
            var historys = JSON.parse(localStorage.getItem('jiankong'));
            //var count = Math.floor((Date.now() - historys.time) / ( 6 * 1000 ));
            /*historys.data.forEach(function (item) {
                item.tdDate2 = parseInt(item.tdDate2) + count;
            });*/
            /*for(var n = 0; n < count; n++){
                historys.data.pop();
                historys.data.unshift(getData(count - n));
            }*/
            historys.time = Date.now();
            localStorage.setItem('jiankong', JSON.stringify(historys))
        }

        $(".jiank .build-time span").html(buildTime[i]);
        $(".jiank table tr").each(function (index) {
            setitem($(this), index)
        });
        function setitem(item, index) {
            var itemData = historys.data[index];
            item.find('td').eq(0).find('img').attr('src', './css/icon_'+itemData.imgSrc+'.png');
            item.find('td').eq(1).html(names[itemData.tdDate1]);
            item.find('td').eq(2).find('span').html( itemData.tdDate2 );
            item.find('td').eq(3).find('span').html( itemData.tdDate3 );
            item.find('td').eq(4).find('span').html( itemData.tdDate4 );

        }
        function getData(index) {
            var tmp = {};
            tmp.imgSrc = Math.random() > 0.3 ? 'cg' : 'jg';
            tmp.tdDate1 = Math.floor(Math.random() * 7);
            tmp.tdDate2 = index * 2 + Math.floor(Math.random() * 2);
            tmp.tdDate3 = Math.floor(Math.random() * 13);
            tmp.tdDate4 = tmp.imgSrc == 'jg' ? Math.ceil(Math.random() * 8) : 0;
            return tmp;
        }
        setInterval(function () {
            $(".jiank table tr").each(function (index) {
                $item = $(this).find('td').eq(2).find('span');
                if(!isNaN(parseInt($item.html()) + 1)){
                    $item.html(parseInt($item.html()) + 1);
                    historys.data[index].tdDate2 = parseInt(parseInt($item.html()) + 1);
                }

            });
            $item = $(".jiank table tr").last();
            historys.data.pop();
            historys.data.unshift(getData(0));
            setitem($item, 0);
            $(".jiank table tr").first().after($item);
            historys.time = Date.now();
            localStorage.setItem('jiankong', JSON.stringify(historys))
        }, timeCount * 12)
    }
    jiankFn();

    // 代码质量概览
    function dmzlglFn() {
        // dmzlglData
        $(".percent-jtdmfx").css('width', dmzlglData[i][0]);
        $(".percent-dycs").css('width', dmzlglData[i][1]);
        $(".percent-aqfx").css('width', dmzlglData[i][2]);
        $(".percent-fgl").css('width', dmzlglData[i][3]);
        option3.title.text = dmzlglData[i][4] + '%';
        option3.series[1].data[0].value = 100 - dmzlglData[i][4];
        option3.series[1].data[1].value = dmzlglData[i][4];
        myChart3.setOption(option3, true);
    }
    dmzlglFn();

    // 代码库
    function dmkFn(type) {
        if(type == '1'){
            var d = [];
            dmkData[1].value.forEach(function (item) {
                d.push(Math.ceil(item / 24))
            })
            dmkData[1].value.forEach(function (item) {
                d.push(Math.ceil(item / 24))
            })
            dmkData[2].value.forEach(function (item) {
                d.push(Math.ceil(item / 24))
            })
            option4.series[0].data = d;
            option4.xAxis[0].data = getDayTime();
        }else if(type == '7'){
            option4.series[0].data = dmkData[i].value.slice(-6);
            option4.xAxis[0].data = dmkData[i].date.slice(-6);
        }else if (type == '30'){
            option4.series[0].data = dmkData[i].value;
            option4.xAxis[0].data = dmkData[i].date;
        }
        myChart4.setOption(option4);

    }
    dmkFn('30');
    // 日均代码提交量
    function rjdmtjl2Fn() {
        $(".rjdmtjl2 div").each(function (n) {
            $(this).find('p').eq(0).html(rjdmtjl2Data[i][n].total + new Date().getHours());
            $(this).find('p').eq(1).html(rjdmtjl2Data[i][n].contri + new Date().getHours());
        })
        setInterval(function () {
            $(".rjdmtjl2 div").each(function (n) {
                $(this).find('p').eq(0).html(rjdmtjl2Data[i][n].total + new Date().getHours());
                $(this).find('p').eq(1).html(rjdmtjl2Data[i][n].contri + new Date().getHours());
            })
            /*var index = $(".rjdmtjl2 .act").index();
            index = (index + 1) % 3;
            $(".rjdmtjl2 div").eq(index).addClass('act').siblings().removeClass('act');*/
            dmkFn('30')
        }, timeCount)
    }
    rjdmtjl2Fn();

    // 开发日志饼图
    function kfrzChartFn() {
        var total = kfrzChart[i].reduce(function(a,b){return a+b;});
        var kfrzChartPassed = (kfrzChart[i][0]*100 / total).toFixed(2);
        var kfrzChartReset = (kfrzChart[i][1]*100 / total).toFixed(2);
        var kfrzChartBlocked = (kfrzChart[i][2]*100 / total).toFixed(2);
        var kfrzChartSkipped = (kfrzChart[i][3]*100 / total).toFixed(2);
        var kfrzChartFailed = (kfrzChart[i][4]*100 / total).toFixed(2);
        var date = new Date();
        var passedPer = date.getHours() * 4;
        $(".passed-num em").html(passedPer + '%');
        $(".passed-item span").css('width', passedPer + '%');

        $(".legend-passed p span").html(kfrzChart[i][0]);
        $(".legend-reset p span").html(kfrzChart[i][1]);
        $(".legend-blocked p span").html(kfrzChart[i][2]);
        $(".legend-skipped p span").html(kfrzChart[i][3]);
        $(".legend-failed p span").html(kfrzChart[i][4]);

        $(".legend-passed em span").html(kfrzChartPassed);
        $(".legend-reset em span").html(kfrzChartReset);
        $(".legend-blocked em span").html(kfrzChartBlocked);
        $(".legend-skipped em span").html(kfrzChartSkipped);
        $(".legend-failed em span").html(kfrzChartFailed);
        option5.series[0].data.forEach(function (item, index) {
            item.value = kfrzChart[i][index];
        })
        myChart5.setOption(option5);
    }
    kfrzChartFn();

});